//
//  ViewController.m
//  19--Homework 汽车之家
//
//  Created by 冀永金 on 16/8/9.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "ViewController.h"
#import "CarTableViewCell.h"
#import "HomeOfCar.h"
#import "UIImageView+WebCache.h"
#import "CustomCell.h"
#import "CustomMainCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableViewOfCar;
@property (nonatomic,strong) NSMutableArray *brandlistMarry;
@property (nonatomic, strong) NSMutableArray<HomeOfCar *> *listMarr;
@property (nonatomic, strong) NSMutableArray *letterMarr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"品牌";
    self.navigationController.navigationBar.translucent = NO;
    [self createTableView];
    [self handleData];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)handleData {
    NSURLSession *urlSession = [NSURLSession sharedSession];
    NSMutableURLRequest *requestHot = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://app.api.autohome.com.cn/autov4.6/cars/brands-a2-pm2-v4.6.1-ts635580073583652962.html"]];
    requestHot.HTTPMethod = @"GET";
   NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:requestHot completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
       dispatch_async(dispatch_get_main_queue(), ^{
           
           NSDictionary *carDic = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers  error:nil];
           
           self.brandlistMarry = carDic[@"result"][@"brandlist"];
           self.letterMarr = [NSMutableArray array];
           self.listMarr = [NSMutableArray array];
           
           for (NSDictionary *dic in _brandlistMarry) {
               
               NSString *str = dic[@"letter"];
               [self.letterMarr addObject:str];
               NSArray *listArr = dic[@"list"];
               for (NSDictionary *dic in listArr) {
                   HomeOfCar *car = [HomeOfCar carWithDic:dic];
                   [self.listMarr addObject:car];
               }
           }
           [self.letterMarr insertObject:@"热" atIndex:0];
           [self.letterMarr insertObject:@"主" atIndex:1];
       [self.tableViewOfCar reloadData];
       });
   }];
    [dataTask resume];
}
- (void)createTableView {
    self.tableViewOfCar = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_tableViewOfCar];
    self.tableViewOfCar.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    self.tableViewOfCar.delegate = self;
    self.tableViewOfCar.dataSource =self;
    [self.tableViewOfCar registerClass:[CarTableViewCell class] forCellReuseIdentifier:@"pool"];
    [self.tableViewOfCar registerClass:[CustomCell class] forCellReuseIdentifier:@"pool1"];
    [self.tableViewOfCar registerClass:[CustomMainCell class] forCellReuseIdentifier:@"pool2"];
  
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.letterMarr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //NSString *str = self.brandlistMarry[section];
    if (section == 0 || section == 1) {
        return 1;
    }
    NSArray *arr = [self.brandlistMarry[section - 2] objectForKey:@"list"];
    return arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 ) {
        CustomCell *cell =[tableView dequeueReusableCellWithIdentifier:@"pool1" forIndexPath:indexPath];
        return cell;
    }else if (indexPath.section == 1) {
        CustomMainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool2" forIndexPath:indexPath];
        return cell;
    }
    else {
   CarTableViewCell  *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
     NSArray *arr = [self.brandlistMarry[indexPath.section - 2] objectForKey:@"list"];
    NSDictionary *dic = [arr objectAtIndex:indexPath.row];
    cell.carLabel.text = dic[@"name"];
    NSString *url = dic[@"imgurl"];
    NSURL *urlStr = [NSURL URLWithString:url];
    [cell.carImage sd_setImageWithURL:urlStr placeholderImage:[UIImage imageNamed:@"concise_icon_favorite_normal"]];
    return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return 300;
    }
    else if (indexPath.section == 1) {
        return 200;
    }
    else {
        return 80;
    }
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"热门搜索";
    }
    else if (section == 1) {
        return @"主打车";
    }
    else {
    return self.letterMarr[section];
    }
}

-(NSArray <NSString *>*)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.letterMarr;
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
